{% if has_window %}
SELECT {% for field in projections %}
{% if field.window %}{{field.value}}{% else %}{{field.alias}}{% endif %}{% if not loop.last %},{% endif %}{% endfor %} FROM (
{% endif %}{% set nonWindowProjections = projections | selectattr('window', 'equalto', false) | list %}
SELECT {% for field in nonWindowProjections %}
    {{field.value}}{% if not loop.last %},{% endif %}{% endfor %}
FROM {{tableReference}}
{% for join in joins %} {{join}} {% endfor %}{% if whereFilter %}
    WHERE {{whereFilter}}{% endif %}{% if groups %}
    GROUP BY
    {% for group in groups %}{{group}}{% if not loop.last %}, {% endif %} {% endfor %}{% endif %}{% if havingFilters %}
    HAVING {{havingFilter}}{% endif %}{% if orderBy %}
    ORDER BY {% for item in orderBy %}
        {{item}}{% if not loop.last %},{% endif %} {% endfor %}{% endif %}
{{ limit }}
{% if has_window %}){% endif %}